//
//  HWMyGiftBagViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/21.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWMyGiftBagViewController.h"
#import "HWLiBaoCell.h"

@interface HWMyGiftBagViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSMutableArray *dataMuArr;
@property(strong,nonatomic) UIImageView *defaultIamegView;
@property(strong,nonatomic) UILabel *wuLabel; //无数据了
 
@end

@implementation HWMyGiftBagViewController


-(UITableView *)mainaTableVie {

    if  (_mainaTableVie == nil) {

        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
      //  _mainaTableVie.backgroundColor = RGB(241, 242, 244);
        
    }
    return   _mainaTableVie ;

}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    
    // 发送网络请求
    [self fasongwangluoqingqiu];
    
    
    //如果没有数据的时候添加一张默认图
    [self zanWuNoDdataIcoImageView];
    
    
    
    
}


- (void)zanWuNoDdataIcoImageView {
    
    self.defaultIamegView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-189)/2,(kScreenH-124)/2-15-60, 189, 124)];
    self.defaultIamegView.image = [UIImage imageNamed:@"details_dealbg_NOdata"];
    [self.mainaTableVie addSubview: self.defaultIamegView];
    
    
    self.wuLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.defaultIamegView.bottom, kScreenW, 15)];
    self.wuLabel.text = @"暂无数据";
    self.wuLabel.font = [UIFont systemFontOfSize:13];
    self.wuLabel.textColor = RGB(102, 102, 102);
    self.wuLabel.textAlignment  = NSTextAlignmentCenter;
    [self.mainaTableVie addSubview: self.wuLabel];
    
    
    
    
}


- (void)fasongwangluoqingqiu {
    
     NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
     
       int r =  [WMUtil getRandomNumber:101 to:998];
       NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],r];
       
       NSString *typeStr =  [NSString stringWithFormat:@"%@",@"0"];
       
       NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
       
       NSString *singMD5 = [WMUtil md5HexDigest:singStr];

      
       NSDictionary *parametes = @{
                                       
                                          @"ios" : @"1",
                                          @"ct" :  @"app",
                                          @"ac" : @"mylb",
                                          @"uid" :uidStr,
                                          @"t" : tStr,
                                      
                                          @"sign" : singMD5,
                                         
                                          };
          
          [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
              
              // [SVProgressHUD dismiss];
      
              
              NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
              
              if ([codeStr intValue] == 1) {
                  
                  
                  self.defaultIamegView.hidden = YES;
                  self.wuLabel.hidden = YES;
                  
          
                  [MBProgressHUD showError:response[@"message"]];
                  
                  self.dataMuArr = response[@"data"];
                  [self.mainaTableVie reloadData];
                  
       
              } else {
                  
                  [MBProgressHUD showError:response[@"message"]];
                  
                  
                                  
                  
              }
           
              
              
          } failure:^(NSString *error) {
              
                  [MBProgressHUD showError:@"领取失败"];
              
          }];
    
    
}



-(void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的礼包";
    [self.view addSubview:self.mainaTableVie];
}




#pragma mark ------------  UITableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    
    return self.dataMuArr.count;
    
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    HWLiBaoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"HWLiBaoCell" owner:nil options:nil].firstObject;
    }
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    
    NSDictionary *dic = self.dataMuArr[indexPath.row];
    
    [cell.icoiamgView sd_setImageWithURL:[NSURL URLWithString:dic[@"icon"]] placeholderImage:nil];
    cell.titleBiaotiLbel.text = [NSString stringWithFormat:@"%@",dic[@"name"]];
    cell.libaoMaLabel.text = [NSString stringWithFormat:@"礼包码：%@",dic[@"card_no"]];
    cell.dateLabel.text = [NSString stringWithFormat:@"领取日期：%@",dic[@"time"]];
    [cell.fuzhiBut addTarget:self action:@selector(fuzhiButTap:) forControlEvents:UIControlEventTouchUpInside];
    

    
    
    return  cell;
    

    
    
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   
    
    return  71;
    
}

- (void)fuzhiButTap:(UIButton *) sender {
    HWLiBaoCell *cell =    (HWLiBaoCell *)[[sender superview] superview];
    NSIndexPath *index  = [self.mainaTableVie indexPathForCell:cell];
    
    NSDictionary *dic = self.dataMuArr[index.row];
 //   dic[@"card_no"]
    //系统级别
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string =  dic[@"card_no"];
    
    [MBProgressHUD showError:@"复制成功"];
    
    
    //应用内单独使用时
//    NSString * strBuildID = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleIdentifier"];
//    UIPasteboard * myPasteboard = [UIPasteboard pasteboardWithName:strBuildID create:YES];
//    myPasteboard.string = @"复制测试数据";
    
    
//    NSString * strBuildID = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleIdentifier"];
//       UIPasteboard * myPasteboard = [UIPasteboard pasteboardWithName:strBuildID create:NO];
//       self.pLabel.text = myPasteboard.string;
}

@end
